<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getScriptList, deleteScript } from '@/api/script'

const router = useRouter()
const scripts = ref([])
const searchQuery = ref('')
const loading = ref(false)

const fetchScripts = async () => {
  loading.value = true
  try {
    const res = await getScriptList({ name: searchQuery.value })
    scripts.value = res.data || []
  } catch (error) {
    console.error('获取脚本列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  fetchScripts()
}

const handleEdit = (script) => {
  router.push({
    name: 'editor',
    params: { id: script.id },
    query: { name: script.name }
  })
}

const handleDelete = async (event, script) => {
  event.stopPropagation() // 阻止事件冒泡，避免触发编辑
  if (confirm(`确定要删除脚本 "${script.name}" 吗？`)) {
    try {
      await deleteScript(script.id)
      alert('删除成功')
      fetchScripts() // 重新加载列表
    } catch (error) {
      console.error('删除失败:', error)
      alert('删除失败: ' + error.message)
    }
  }
}

onMounted(() => {
  fetchScripts()
})
</script>

<template>
  <div class="script-list-container" :class="{ 'light-theme': !isDarkTheme }">
    <div class="list-header">
      <h1>脚本列表</h1>
      <div class="search-bar">
        <input 
          type="text" 
          v-model="searchQuery"
          placeholder="搜索脚本名称..."
          @keyup.enter="handleSearch"
        >
        <button @click="handleSearch" class="search-button">
          🔍 搜索
        </button>
      </div>
      <button @click="router.push('/editor')" class="new-button">
        ✨ 新建脚本
      </button>
    </div>

    <div class="scripts-container">
      <div v-if="loading" class="loading">
        加载中...
      </div>
      <div v-else-if="scripts.length === 0" class="empty-state">
        暂无脚本
      </div>
      <div v-else class="script-grid">
        <div 
          v-for="script in scripts" 
          :key="script.id" 
          class="script-card"
          @click="handleEdit(script)"
        >
          <div class="script-info">
            <h3>{{ script.name }}</h3>
            <p class="script-type">{{ script.language }}</p>
          </div>
          <div class="script-meta">
            <span class="script-date">{{ script.createTime }}</span>
            <button 
              class="delete-button"
              @click="handleDelete($event, script)"
              title="删除脚本"
            >
              🗑️
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.script-list-container {
  padding: 24px;
  min-height: 100vh;
  background-color: #1e1e1e;
  color: #fff;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  gap: 16px;
}

.search-bar {
  display: flex;
  gap: 8px;
  flex: 1;
  max-width: 500px;
}

.search-bar input {
  flex: 1;
  padding: 8px 16px;
  border: 1px solid #404040;
  border-radius: 4px;
  background: #2d2d2d;
  color: #fff;
  font-size: 14px;
}

.search-button {
  padding: 8px 16px;
  background-color: #2d5af2;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.new-button {
  padding: 8px 16px;
  background-color: #2ea043;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.script-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.script-card {
  padding: 16px;
  background: #2d2d2d;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.script-card:hover {
  transform: translateY(-2px);
  background: #363636;
}

.script-info h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 500;
}

.script-type {
  font-size: 12px;
  color: #888;
  margin: 0;
}

.script-meta {
  margin-top: 12px;
  font-size: 12px;
  color: #666;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.delete-button {
  padding: 4px 8px;
  background-color: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  color: #666;
  transition: all 0.3s ease;
}

.delete-button:hover {
  color: #ff4444;
  background-color: rgba(255, 68, 68, 0.1);
}

.light-theme .delete-button:hover {
  background-color: rgba(255, 68, 68, 0.05);
}

.loading, .empty-state {
  text-align: center;
  padding: 40px;
  color: #666;
}

/* Light theme styles */
.light-theme {
  background-color: #ffffff;
  color: #000;
}

.light-theme .search-bar input {
  background: #f5f5f5;
  border-color: #ddd;
  color: #000;
}

.light-theme .script-card {
  background: #f5f5f5;
}

.light-theme .script-card:hover {
  background: #eaeaea;
}

@media (max-width: 640px) {
  .list-header {
    flex-direction: column;
    align-items: stretch;
  }

  .search-bar {
    max-width: none;
  }

  .script-grid {
    grid-template-columns: 1fr;
  }
}
</style> 